﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Read.API.Command;
using B.S.BaseData.Read.API.Command.LeaveCommand;
using B.S.BaseData.Read.API.Command.MenuCommand;
using B.S.BaseData.Read.API.Command.RoleCommand;
using B.S.BaseData.Read.API.Command.RoleMenuCommand;
using B.S.BaseData.Read.API.Command.UserCommand;
using B.S.BaseData.Read.API.DTO;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;


namespace B.S.BaseData.Read.API.Controllers
{
    /// <summary>
    /// API控制器-读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
   // [Authorize]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator"></param>
        public UserController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<APIResult<User>> Login([FromQuery] LoginUserCommand request)
        {
          
            return mediator.Send(request);
        }

        #region 用户信息

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIPaging<UserDTO>> GetUser([FromQuery] GetUserCommand request)
        {
            return mediator.Send(request);

        }
        /// <summary>
        /// 反填用户信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>任务</returns>
        [HttpGet]
        public Task<APIResult<User>> GetUsers([FromQuery] GetUsersCommand request)
        {
            return mediator.Send(request);
        }
        #endregion

        #region 角色信息(分配用户)

        /// <summary>
        /// 角色显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<Role>>> GetRole([FromQuery] GetRoleCommand request)
        {
            return mediator.Send(request);
        }


        /// <summary>
        /// 角色名称唯一验证
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<object>> OneRoleName([FromQuery] OnenessRoleNameCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 角色反填(id查询)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<Role>> RoleFanTian([FromQuery] RoleFanTianCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 根据部门Id查询用户名称
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<User>>> GetUserName([FromQuery] GetDepIdUserCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 通过角色ID查询用户名称
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<UserNameDTOs>>> GetUserNameRoleId([FromQuery] GetUserNameRoleidCommand request)
        {
            return mediator.Send(request);
        }

        #endregion


        #region 菜单信息
        /// <summary>
        /// 查询菜单列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<MenuDTO>>> QueryMenu([FromQuery] QueryMenuCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 查询是否有子集
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<int>> QueryPid([FromQuery] QueryPidCommand request)
        {
            return mediator.Send(request);
        }

        #endregion


        #region 角色菜单信息

        /// <summary>
        /// 查询角色树状信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>任务</returns>
        [HttpGet]
        public Task<APIResult<List<Role>>> QueryRole([FromQuery] QueryRoleCommand request)
        {
            return mediator.Send(request);

        }


        /// <summary>
        /// 查询菜单树状结构
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<RoleMenuDTO>>> QueryRoleMenu([FromQuery] QueryRoleMenuCommand request)
        {
            return mediator.Send(request);
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<object>> FindMenu([FromQuery] FindMenuCommand request)
        {
            return mediator.Send(request);
        }
        #endregion


        /// <summary>
        /// 查询导航信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<NavigationDTO>>> QueryNavigation([FromQuery] QueryNavigationCommand request)
        {
            var sub = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var rid = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
            request.uid = sub;
            request.rid = rid;
            return mediator.Send(request);
        }

        /// <summary>
        /// 查询经理名字
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<string>> QueryPowerName([FromQuery] QueryNameCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 查询请假列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIPaging<LeaveDTO>> QueryLeave([FromQuery] QueryLeaveCommand request)
        {
            return mediator.Send(request);
        }

        /// <summary>
        /// 查询原因
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<Record>>> QueryReason([FromQuery]QueryReasonCommand request)
        {
            return mediator.Send(request);
        }
    }
}
